package com.ljpz.common.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

/**
 * 模拟js中回调的思想
 * Created by liujunchen on 2016/12/17.
 */

@Service
public class RedisService {
    @Autowired(required = false) //从spring容器中查找bean ， 找到就注入，找不到就忽略
    private ShardedJedisPool shardedJedisPool ;
    //封装思想
    private   <T>  T   execute(Function<T,ShardedJedis> fun ){
        ShardedJedis   shardedJedis= null ;
        try {
            //从连接中获取jedis分片对象
            shardedJedis= shardedJedisPool.getResource() ;
            return  fun.callback(shardedJedis) ;
        }finally {
            if (null!=shardedJedis){
                //关闭，检测连接是否有效，有效则放回到连接池中去，  无效则重置状态
                shardedJedis.close();
            }
        }
    }
    public  String    set(final String  key , final String value ){
        return  this.execute(new Function<String, ShardedJedis>() {
            @Override
            public String callback(ShardedJedis shardedJedis) {
                return shardedJedis.set(value, key);
            }
        })  ;
    /*    ShardedJedis  shardedJedis =   null  ;
        try {
            //从连接池中获取jedis 分片对象
            shardedJedis = shardedJedisPool.getResource() ;
            return   shardedJedis.set(key, value) ;
        }finally {
            if (null!=shardedJedis){
                //关闭，检测连接是否有效，有效则放回到连接池中， 无效则重置状态
                shardedJedis.close();
            }
        }*/
    }
    public  String     get(final String  key  ){
  /*      ShardedJedis  shardedJedis =   null  ;
        try {
            //从连接池中获取jedis 分片对象
            shardedJedis = shardedJedisPool.getResource() ;
            return   shardedJedis.get(key) ;
        }finally {
            if (null!=shardedJedis){
                //关闭，检测连接是否有效，有效则放回到连接池中， 无效则重置状态
                shardedJedis.close();
            }
        }*/
        return  this.execute(new Function<String, ShardedJedis>() {
            @Override
            public String callback(ShardedJedis shardedJedis) {
                    return  shardedJedis.get(key);
            }
        });
    }

    /**
     *
     * 删除
     * @param key
     * @return
     */
    public  Long    del(final  String key ){
        return   this.execute(new Function<Long , ShardedJedis>() {
            @Override
            public Long  callback(ShardedJedis shardedJedis) {
                return shardedJedis.del(key);
            }
        }) ;
    }
    //设置生存时间
    public  Long   expire(final   String key ,  final  Integer   seconds ){
        return   this.execute(new Function<Long, ShardedJedis>() {
            @Override
            public Long callback(ShardedJedis shardedJedis) {
                return  shardedJedis.expire(key,seconds) ;
            }
        });
    }

    /**
     * 设置生存时间， 单位为秒
     * @param key
     * @param seconds
     * @param value
     * @return
     */

    public  String set(final  String key , final  Integer seconds  , final   String value ){
        return  this.execute(new Function<String, ShardedJedis>() {
            @Override
            public String callback(ShardedJedis shardedJedis) {
                String   str=  shardedJedis.set(key,value);
                shardedJedis.expire(key,seconds);
                return  str ;
            }
        });
    }


}
